﻿using System;
using System.Collections.Generic;
using System.Text;
using YouHP.Utility;
using YouHP.BLL;

namespace YouHP.Pages
{
    public class UserAjax : BasePage
    {
        protected override void ShowPage()
        {
            base.ShowPage();

            string type = RequestTools.GetQueryString("f");
            switch (type)
            {
                case "chkusr":
                    CheckUserName();
                    break;
                case "chkemail":
                    CheckEmail();
                    break;
                default:
                    break;
            }

        }

        public void CheckUserName()
		{
		    string usr = RequestTools.GetQueryString("usr");
            string result = "1";
            bool exist = UserBLL.Instance.CheckExsitUserName(usr);
            if(exist == false) result = "0"; 

			StringBuilder sb = new System.Text.StringBuilder();
            sb.Append(result);
			ResponseXML(sb);
        }

        public void CheckEmail()
        {
            string email = RequestTools.GetQueryString("email");
            string result = "1";
            bool exist = UserBLL.Instance.CheckExsitEmail(email);
            if (exist == false) result = "0"; 
           
            StringBuilder sb = new System.Text.StringBuilder();
            sb.Append(result);
            ResponseXML(sb);
        }

        private void ResponseXML(System.Text.StringBuilder xmlnode)
        {
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.ContentType = "Text/html";
            System.Web.HttpContext.Current.Response.Expires = 0;

            
            System.Web.HttpContext.Current.Response.Write(xmlnode.ToString());
            System.Web.HttpContext.Current.Response.End();
        }
    }
}
